package com.ankiang.mykeys;


import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Random;

/***
 * ADAPTER
 */

public class ExploreAdapter extends ArrayAdapter<JSONObject> {

    private static final String TAG = "ExploreAdapter";

    static class ViewHolder {
        //DynamicHeightTextView txtShortcutDes;
        public TextView btnExplore;

        public String url;
        public String id;
        public String desc;
        public String category;
        public String appearance;
        public String clickCount;
        public String mode;
    }

    public ImageLoader imageLoader; //用来下载图片的类，后面有介绍

    private final LayoutInflater mLayoutInflater;
    private final Random mRandom;
    private final ArrayList<Integer> mBackgroundColors;

    private static final SparseArray<Double> sPositionHeightRatios = new SparseArray<Double>();

    public ExploreAdapter(final Context context, final int textViewResourceId) {
        super(context, textViewResourceId);
        mLayoutInflater = LayoutInflater.from(context);
        mRandom = new Random();
        mBackgroundColors = new ArrayList<>();
        mBackgroundColors.add(R.color.orange);
        mBackgroundColors.add(R.color.green);
        mBackgroundColors.add(R.color.blue);
        mBackgroundColors.add(R.color.yellow);
        mBackgroundColors.add(R.color.grey);
        imageLoader = new ImageLoader(context.getApplicationContext());
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        ViewHolder vh;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.explore_list_item, parent, false);
            vh = new ViewHolder();
            vh.btnExplore = (TextView) convertView.findViewById(R.id.explore_btn);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        double positionHeight = getPositionRatio(position);
        int backgroundIndex = position >= mBackgroundColors.size() ?
                position % mBackgroundColors.size() : position;

        convertView.setBackgroundResource(mBackgroundColors.get(backgroundIndex));

        Log.d(TAG, "getView position:" + position + " h:" + positionHeight);


        JSONObject obj = getItem(position);
        vh.id = Utils.getStringFromJson(obj, "id");
        vh.desc = Utils.getStringFromJson(obj, "desc");
        vh.category = Utils.getStringFromJson(obj, "category");
        vh.category = Utils.getStringFromJson(obj, "category");
        vh.clickCount = Utils.getStringFromJson(obj, "clickCount");
        vh.appearance = Utils.getStringFromJson(obj, "appearance");
        vh.mode = Utils.getStringFromJson(obj, "mode");
        vh.url = Utils.getStringFromJson(obj, "url");
        if (vh.btnExplore != null) {
            vh.btnExplore.setText(Utils.getStringFromJson(obj, "name"));
            Log.i(TAG, obj.toString());

            vh.btnExplore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {

//                ExploreAdapter.ViewHolder vh = (ViewHolder) getView(position, v, parent).getTag();
//                ExploreAdapter.ViewHolder vh = (ViewHolder) v.getTag();
//                if (vh == null) {
//                    Toast.makeText(getContext(), "打开网址失败: vh=null ", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//                String url = vh.url;
//
//                if (Utils.isEmpty(url)) {
//
//                    Toast.makeText(getContext(), "打开网址失败 " +
//                            url, Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//                Intent intent = new Intent();
//                intent.setClass(getContext(), WebViewActivity.class);
//                Log.i("url", url);
//
//                intent.putExtra("url", url);
//
//                getContext().startActivity(intent);
                }
            });
        }


        return convertView;
    }

    private double getPositionRatio(final int position) {
        double ratio = sPositionHeightRatios.get(position, 0.0);
        // if not yet done generate and stash the columns height
        // in our real world scenario this will be determined by
        // some match based on the known height and width of the image
        // and maybe a helpful way to get the column height!
        if (ratio == 0) {
            ratio = getRandomHeightRatio();
            sPositionHeightRatios.append(position, ratio);
            Log.d(TAG, "getPositionRatio:" + position + " ratio:" + ratio);
        }
        return ratio;
    }

    private double getRandomHeightRatio() {
        return (mRandom.nextDouble() / 2.0) + 1.0; // height will be 1.0 - 1.5 the width
    }
}